<?php

namespace app\admin\controller\erm;

use app\admin\controller\Controllers;
use app\admin\service\erm\Sidebar as SidebarService;
use app\admin\model\erm\Sidebar as SidebarModel;

class Sidebar extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(SidebarService $service)
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\erm\Sidebar.index');
        $list = $service->indexPaginate($get);
        $config = config('field.sidebar');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'status' => $config['status']
        ]);
        return $this->fetch('erm/sidebar/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $config = config('field.sidebar');
        $this->assign([
            'status' => $config['status']
        ]);
        return $this->fetch('erm/sidebar/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(SidebarService $service)
    {
        $post = $this->request->only(['name','sort','status'], 'post');
        $this->validate($post, 'app\admin\validate\erm\Sidebar.create');
        $sidebarId = $service->create($post);
        if ( false === $sidebarId ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(int $id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(SidebarModel $model)
    {
        $config = config('field.sidebar');
        $this->assign([
            'data' => $model,
            'status' => $config['status']
        ]);
        return $this->fetch('erm/sidebar/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id, SidebarService $service)
    {
        $put = $this->request->only(['name','sort','status'], 'put');
        $put['id'] = $id;
        $this->validate($put, 'app\admin\validate\erm\Sidebar.update');
        $update = $service->save($id, $put);
        if ( $update === false ) {
            return failed('更新失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, SidebarService $service)
    {
        if ( $id <= 0 ) {
            return failed('参数错误');
        }
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 状态修改
     * @author zx
     * @date 2019-12-17
     * @return \think\Response
     */
    public function status(SidebarService $service, SidebarModel $model)
    {
        return $service->status($model);
    }

    /**
     * 检测侧边栏名称是否唯一
     * @author zx
     * @date 2019-12-17
     * @return \think\Response
     */
    public function only( SidebarService $service )
    {
        $get = $this->request->only(['id','name'], 'get');
        $this->validate($get, 'app\admin\validate\erm\Sidebar.only');
        $bool = $service->only($get);
        return json($bool);
    }

    /**
     * 批量删除
     * @author zx
     * @date 2019-12-17
     * @return \think\Response
     */
    public function destory(SidebarService $menus)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\erm\Sidebar.destory');
        if ( $menus->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
